package xyz.luhui1hao.sell.service.impl;
import com.lly835.bestpay.enums.BestPayTypeEnum;

import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.BestPayService;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.luhui1hao.sell.dto.OrderDTO;
import xyz.luhui1hao.sell.enums.ResultEnum;
import xyz.luhui1hao.sell.exception.SellException;
import xyz.luhui1hao.sell.service.OrderService;
import xyz.luhui1hao.sell.service.PayService;
import xyz.luhui1hao.sell.utils.MathUtil;

import java.math.BigDecimal;

import static com.lly835.bestpay.enums.BestPayTypeEnum.WXPAY_H5;

/**
 * @ClassName PayServiceImpl
 * @Description TODO
 * @Author luhui1hao
 * @Date 2018/9/7 上午11:28
 * @Version 1.0
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {

    private static final String ORDER_NAME = "微信点餐订单";

    @Autowired
    private BestPayServiceImpl bestPayService;
    @Autowired
    private OrderService orderService;

    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setOpenid(orderDTO.getBuyerOpenId());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(WXPAY_H5);

        PayResponse payResponse = bestPayService.pay(payRequest);

        return payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {
        //1.验证签名
        //2.支付的状态
        //3.支付的金额
        //4.支付人（下单人 == 支付人）看业务具体需求

        PayResponse payResponse = bestPayService.asyncNotify(notifyData);

        //查询订单
        OrderDTO orderDTO = orderService.findOne(payResponse.getOrderId());
        if(orderDTO == null){
            log.error("【微信支付】订单不存在");
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }

        //判断金额是否一致
        if(!MathUtil.equals(orderDTO.getOrderAmount().doubleValue(), payResponse.getOrderAmount())){
            log.error("【微信支付】金额校验不通过");
            throw new SellException(ResultEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }

        //修改订单的支付状态
        orderService.pay(orderDTO);
        return payResponse;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        //微信h5支付
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());

        RefundResponse refundResponse = bestPayService.refund(refundRequest);

        return refundResponse;
    }
}
